package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.app.service.WhiteListPhoneService;
import com.junzhiit.nengji.app.web.exception.MobilePhoneNotFoundException;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.enums.LoginType;

@Service
public class WhiteListPhoneServiceImpl extends BaseServiceImpl implements WhiteListPhoneService {

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Override
	public ResultVO findAll(String sessionId) {
		ResultVO result = new ResultVO();
		String systemId = getSystemId(sessionId);

		String key = DIYKey.getWhiteListPhoneKey(systemId);
		List<String> list = systemUserCacheDAO.getListForString(key);
		
		result.setData(list);
		return result;
	}

	public String getSystemId(String sessionId) {
		String sessionKey = DIYKey.getSessionHashKey(sessionId);
		SystemUserSessionVO session = systemUserCacheDAO.getHash(sessionKey, SystemUserSessionVO.class);
		String systemId = "root";
		if (session.getLoginType() == LoginType.SHOP.toIntType()) {
			systemId = session.getSystemId();
		}
		return systemId;
	}
	
	@Override
	public ResultVO checkWhiteListPhone(String sessionId, String phone) {
		ResultVO result = new ResultVO();
		boolean flag=false;
		String systemId = getSystemId(sessionId);
		String key = DIYKey.getWhiteListPhoneKey(systemId);
		List<String> list = systemUserCacheDAO.getListForString(key);
		
		if( list==null || list.isEmpty()){
			throw new MobilePhoneNotFoundException("找不到手机号码");
		}
		
		
		for (String string : list) {
			if (string.equals(phone)) {
				flag=true;
			}
		}
		
		if(flag==false){
			throw new MobilePhoneNotFoundException("找不到手机号码");
		}
		return result;
	}

	@Override
	public ResultVO addWhiteListPhone(String sessionId, String phone) {

		String systemId = getSystemId(sessionId);

		ResultVO result = new ResultVO();
		String key = DIYKey.getWhiteListPhoneKey(systemId);
		List<String> list = systemUserCacheDAO.getListForString(key);
		if (list != null && !list.isEmpty()) {
			for (String string : list) {
				if (string.equals(phone)) {
					return result;
				}
			}
			result.setData(systemUserCacheDAO.listAdd(key, phone));
		} else {
			list = new ArrayList<String>();
			list.add(phone);
			boolean is = systemUserCacheDAO.addListForString(key, list);
			result.setData(is);
			if (is) {
				logger.info("add cache for key:" + DIYKey.getWhiteListPhoneKey(systemId));
			}
		}
		return result;
	}

	@Override
	public ResultVO delWhiteListPhone(String sessionId, String phone) {

		String systemId = getSystemId(sessionId);

		ResultVO result = new ResultVO();
		String key = DIYKey.getWhiteListPhoneKey(systemId);
		List<String> list = systemUserCacheDAO.getListForString(key);
		for (String string : list) {
			if (string.equals(phone)) {
				result.setData(list.remove(string));
				break;
			}
		}
		systemUserCacheDAO.addListForString(key, list);
		return result;
	}

}
